package com.luckmgr.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.luckmgr.dto.ActivityDTO;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.LevelPrize;
import com.luckmgr.entity.db.LuckNumber;
import com.luckmgr.entity.db.LuckRecord;
import com.luckmgr.entity.db.Message;
import com.luckmgr.entity.db.Prize;
import com.luckmgr.service.ActivityLevelService;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LevelPrizeService;
import com.luckmgr.service.LuckRecordService;
import com.luckmgr.service.LuckUserService;
import com.luckmgr.service.MessageService;
import com.luckmgr.service.PrizeService;
import com.luckmgr.system.SystemData;

@Component
public class ActivityCore extends Thread {
	private Logger logger = Logger.getLogger(ActivityCore.class);
	Long intervalTime = 3L;

	@Autowired
	ActivityService activityServiceImpl;
	
	@Autowired
	PrizeService prizeServiceImpl;
	
	@Autowired
	ActivityLevelService activityLevelServiceImpl;
	
	@Autowired
	LevelPrizeService levelPrizeServiceImpl;

	@Autowired
	LuckUserService luckUserService;
	
	@Autowired
	LuckRecordService luckRecordService;
	
	@Autowired
	MessageService messageService;
	
	@Override
	public void run() {
		while (true) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("status", 1);
				List<Activity> activitylist = activityServiceImpl
						.selectList(map);
				
				
				 
				
				
				if (null != activitylist) {
					for (String key : SystemData.activitys.keySet()) {
						
						 Message  message = SystemData.getMessage(key);
						 if(message == null){
							  message =  messageService.selectBySeriesCode(key);
							 if(null != message){
								 SystemData.addMessage(message);
							 }
						 }
						
						 
						 
						ActivityDTO activityDTO = SystemData.activitys.get(key);
						boolean ishave = false;
						for (int i = 0; i < activitylist.size(); i++) {
							Activity activity = activitylist.get(i);
							if (activityDTO.getActivity().getId().equals(activity
									.getId())) {
								ishave = true;
								activitylist.remove(i);
							}
						}

						if (!ishave) {
							SystemData.activitys.remove(key);
						}
					}
					
					for(Activity activity : activitylist){
						initActivity(activity);
					}
					
				} else {
					Iterator<String> iterator = SystemData.activitys.keySet()
							.iterator();
					while (iterator.hasNext()) {
						String actkey = iterator.next();
						SystemData.activitys.remove(actkey);
						SystemData.lottoRecords.remove(actkey);
					}
				}
				Thread.sleep(intervalTime * 1000);
			} catch (Exception e) {
				logger.info(e.getMessage(), e);
			}
		}
	}
	
	
	private void initActivity(Activity activity){
		ActivityDTO activityDTO = new ActivityDTO();
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("aid", activity.getId());
		
		List<Prize> prizes = prizeServiceImpl.selectList(map);
		if(null == prizes) return;	//奖品为空不处理
		List<ActivityLevel> activityLevels = activityLevelServiceImpl.findLevelByAid(activity.getId());
		if(null == activityLevels) return;	//奖品组为空不处理
		for(ActivityLevel activityLevel:activityLevels){
			Map<String,Object> leprizemap = new HashMap<String, Object>();
			leprizemap.put("lid", activityLevel.getId());
			
			List<LevelPrize> listPrize = levelPrizeServiceImpl.selectList(leprizemap);
			LuckNumber luckNumberDTO = luckUserService.findLuckNumber(activityLevel.getId(),activityLevel.getLotteryNum());
			if(null != luckNumberDTO){
				activityLevel.setSumLuckNum(new AtomicInteger(luckNumberDTO.getUserunt()*activityLevel.getLotteryNum()-luckNumberDTO.getAskunt()));
			}
			if(null != listPrize){
				activityLevel.setLeprizes(listPrize);
			}
		}
		Map<Integer,Prize> prizeMap = new HashMap<Integer, Prize>();
		for(Prize prize : prizes){
			prizeMap.put(prize.getId(), prize);
		}
		
		Map<Integer,ActivityLevel> activityLevelMap = new HashMap<Integer, ActivityLevel>();
		for(ActivityLevel activityLevel:activityLevels){
			activityLevelMap.put(activityLevel.getId(), activityLevel);
		}
		
		activityDTO.setActivity(activity);
		activityDTO.setPrizes(prizeMap);
		activityDTO.setAcLevels(activityLevelMap);
		
		SystemData.activitys.put(activityDTO.getActivity().getSeriesCode(), activityDTO);
		
		
		 List<LuckRecord> list = luckRecordService.findRecentlyRecord(activity.getId());
		 
		 if(null != list){
			 for(LuckRecord luckRecord : list){
				 SystemData.addLottoRecord(activity.getSeriesCode(), luckRecord);
			 }
		 }
		 
		 
	}
	
	
	
}
